package com.nmc.webapp.action;

import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.nmc.service.manager.BirthManager;
import com.nmc.service.model.StatisticsModel;

public class JsonResponseAction extends BaseAction{

	//String jsonString="{[[\'Male\', 48.0],[\'Female\', 52.0],]";
	
	String jsonString="{\"Male\": 35.0, \"Female\":65.0}";
	
	
	String jsonStringHospitalHome="[ { \"name\": \"Male\",\"data\":[5, 0, 0, 7, 2,8] } , { \"name\": \"Female\",\"data\":[5, 0, 9, 7, 2,8] }]";
	
	//This should return the Zone wise Male Female List
	String jsonStringAdminHome="[ { \"name\": \"Male\",\"data\":[5, 0, 0, 7, 2,8] } , { \"name\": \"Female\",\"data\":[5, 0, 9, 7, 2,8] }]";
	
	private BirthManager birthManager;

	private InputStream inputStream;

	private int hospitalId;
	private int zoneId;
	
    public InputStream getInputStream() {
        return inputStream;
    }

    public String execute() throws Exception {
    	//Pass
    	StatisticsModel statisticsModel = birthManager.getMaleFemaleCount(6);
    	jsonString="{\"Male\": "+statisticsModel.getMaleCount()+".0, \"Female\":"+statisticsModel.getFemaleCount()+".0}";
        inputStream = new StringBufferInputStream(jsonString);
        return SUCCESS;
    }

	
    public String hospitalHome() throws Exception {
    	
    	Map<String, ArrayList<Integer>>  map = birthManager.getHospitalMaleFemaleCountByMonths(hospitalId,6);
        ArrayList<Integer> maleList = map.get("Male");
        ArrayList<Integer> femaleList = map.get("Female");
        
        jsonStringHospitalHome="[ { \"name\": \"Male\",\"data\":["+maleList.get(0)+","+maleList.get(1)+","+maleList.get(2)+","+maleList.get(3)+","+maleList.get(4)+","+maleList.get(5)+"] } ," +
        		" { \"name\": \"Female\",\"data\":["+femaleList.get(0)+","+femaleList.get(1)+","+femaleList.get(2)+","+femaleList.get(3)+","+femaleList.get(4)+","+femaleList.get(5)+"]}]";
    	inputStream = new StringBufferInputStream(jsonStringHospitalHome);
        return SUCCESS;
    }

    public String zoneHome() throws Exception {
    	
    	Map<String, ArrayList<Integer>>  map = birthManager.getZoneMaleFemaleCountByMonths(zoneId,6);
        ArrayList<Integer> maleList = map.get("Male");
        ArrayList<Integer> femaleList = map.get("Female");
        
        jsonStringHospitalHome="[ { \"name\": \"Male\",\"data\":["+maleList.get(0)+","+maleList.get(1)+","+maleList.get(2)+","+maleList.get(3)+","+maleList.get(4)+","+maleList.get(5)+"] } ," +
        		" { \"name\": \"Female\",\"data\":["+femaleList.get(0)+","+femaleList.get(1)+","+femaleList.get(2)+","+femaleList.get(3)+","+femaleList.get(4)+","+femaleList.get(5)+"]}]";
    	inputStream = new StringBufferInputStream(jsonStringHospitalHome);
        return SUCCESS;
    }

    public String adminHome() throws Exception {
    	List<Object[]> objList = birthManager.getAllZonesMaleFemaleCounts(6);
    	String maleStr="";
    	String femaleStr ="";
    	int size = objList.size();
    	if(objList != null && size>0){
    		int cnt=0;
    		
			for(Object[] obj:objList){
				cnt = cnt + 1;
				if(cnt == size){
					maleStr=maleStr+obj[2]+"";
					femaleStr=femaleStr+obj[3]+"";
				}else{
				maleStr=maleStr+obj[2]+",";
				femaleStr=femaleStr+obj[3]+",";
				}
			}
			jsonStringAdminHome="[ { \"name\": \"Male\",\"data\":["+maleStr+"] } , { \"name\": \"Female\",\"data\":["+femaleStr+"] }]";
			inputStream = new StringBufferInputStream(jsonStringAdminHome);
    	}
    	
    	return SUCCESS;
    }
	public void setBirthManager(BirthManager birthManager) {
		this.birthManager = birthManager;
	}

	public BirthManager getBirthManager() {
		return birthManager;
	}

	/**
	 * @return the hospitalId
	 */
	public int getHospitalId() {
		return hospitalId;
	}

	/**
	 * @param hospitalId the hospitalId to set
	 */
	public void setHospitalId(int hospitalId) {
		this.hospitalId = hospitalId;
	}

	/**
	 * @return the zoneId
	 */
	public int getZoneId() {
		return zoneId;
	}

	/**
	 * @param zoneId the zoneId to set
	 */
	public void setZoneId(int zoneId) {
		this.zoneId = zoneId;
	}
    
}
